package com.crm.utils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0
 * @author: CRM_SYSTEM
 * @date: 2022/4/7 21:57
 * @Description:
 */
public class DateUtils {

    public static final String dtfm = "yyyy-MM";
    public static final String dtfd = "yyyy-MM-dd HH:mm:ss";

    public static List<LocalDateTime> getLastMouth(LocalDate localDate, Integer count) {
        LocalDate afterDate = localDate.minusMonths(count);
        // 获取倒数30天
        List<LocalDateTime> localDateTimes = new ArrayList<>(30);
        for (LocalDate currentdate = afterDate; currentdate.isBefore(localDate) || currentdate.isEqual(localDate); currentdate = currentdate.plusMonths(1)) {
            localDateTimes.add(LocalDateTime.of(currentdate.getYear(), currentdate.getMonth(), 1, 0, 0));
        }
        return localDateTimes;
    }

    public static List<String> getStrLastMouth(LocalDate localDate, Integer count) {
        List<String> result = new ArrayList<>();
        List<LocalDateTime> localDateTimes = getLastMouth(localDate, count);
        for (LocalDateTime localDateTime : localDateTimes) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dtfm);
            String format = localDateTime.format(formatter);
            result.add(format);
        }
        return result;
    }

    public static void main(String[] args) {
        List<String> lastMouth = getStrLastMouth(LocalDate.now(), 12);
        for (String localDateTime : lastMouth) {
            System.out.println(localDateTime);
        }
    }
}

